<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2019/7/14
 * Time: 15:52
 */

namespace app\admin\controller;

use think\Db;

class Gateway extends Common
{
    /**
     * 网关列表
     */
    public function gateway()
    {
        if (request()->isPost()) {
            $key = input('post.key') ? input('key') : '';
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $list = Db('nyd_gateway')
                ->alias('a')
                ->join('nyd_house b','a.house_id = b.id')
                ->field('a.id,a.is_bad,a.is_open,b.house_name,a.gateway_id,a.gateway_name')
                ->where('a.gateway_id|b.house_name|a.gateway_name', 'like', "%" . $key . "%")
                ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                ->toArray();
            foreach ($list['data'] as $k => $v) {
                if ($list['data'][$k]['is_bad'] == 1) {
                    $list['data'][$k]['is_bad'] = '正常';
                } else if ($list['data'][$k]['is_bad'] == 2) {
                    $list['data'][$k]['is_bad'] = '故障';
                }
            }
            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        return $this->fetch('gateway/gateway_list');
    }

    /**
     * 网关状态开关
     */
    public function gatewayState()
    {
        $id = input('post.id');
        $is_open = input('post.is_open');
        if (empty($id)) {
            return $this->error('设备不存在!');
        }
        db('nyd_gateway')->where('id', $id)->update(['is_open' => $is_open]);
        return $this->success('状态修改成功');
    }

    /**
     * @return mixed|void
     * 网关tianjia
     */
    public function addGateway()
    {

        if (request()->isPost()) {
            $data = input('post.');
            $re = Db::name('nyd_gateway')->insert($data);
            if ($re) {
                $this->success('添加成功');
            } else {
            }
            $this->error('添加失败');
        } else {
            $house = Db::name('nyd_house')->select();
            $this->assign('house', $house);
            return $this->fetch('gateway/addGateway');
        }
    }

    /**
     * @return mixed|void
     * 网关编辑
     */
    public function gatewayEdit()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $re = Db::name('nyd_gateway')->where('id', $data['id'])->update($data);
            if ($re) {
                $this->success('修改成功');
            } else {
                $this->error('修改失败');
            }

        }
        $id = input('get.id');
        if (empty($id)) {
            return $this->error('网关不存在!');
        }

        $house= Db::name('nyd_house')->select();
        $this->assign('house',$house);
        $result = Db::name('nyd_gateway')
            ->alias('a')
            ->join('nyd_house b','a.house_id = b.id')
            ->field('a.id,a.is_bad,a.house_id,b.house_name,a.gateway_id,a.gateway_name')
            ->where('a.id', $id)->find();
        $this->assign('info', $result);
        $this->assign('id', $id);
        return $this->fetch('gateway/gatewayEdit');
    }

    /**
     * 删除
     */
    public function gatewayDel()
    {
        $id = input('post.id');
        if (empty($id)) {
            return $this->error('大棚不存在!');
        }
        db('nyd_gateway')->where('id', $id)->delete();
        return $this->success('删除成功');
    }


}